package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.Reditor;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Strings;

public class REditorController extends Controller {

  public void add() {
    JSONObject req = Strings.toJSONObject(this);
    final JSONObject data = Strings
        .getJSONObject(req, "data", Consts.EMPTY_OBJECT);
    final String content = Strings.getString(req, "content", "");

    ModelResult result = Reditor.add(data, content);
    renderJson(result.toJson());
  }

  public void update() {
    JSONObject req = Strings.toJSONObject(this);
    final String code = Strings.getString(req, "code", "");
    final JSONObject data = Strings
        .getJSONObject(req, "data", Consts.EMPTY_OBJECT);
    final String content = Strings.getString(req, "content", "");

    ModelResult result = new ModelResult(Result.kBadParam);

    if (StrKit.notBlank(code)) {
      result = Reditor.update(code, data, content);
    }

    renderJson(result.toJson());
  }


  public void delete() {
    JSONObject req = Strings.toJSONObject(this);
    final String code = Strings.getString(req, "code", "");

    ModelResult result = new ModelResult(Result.kBadParam);

    if (StrKit.notBlank(code)) {
      result = Reditor.delete(code);
    }

    renderJson(result.toJson());
  }

  public void query() {
    JSONObject req = Strings.toJSONObject(this);
    final String code = Strings.getString(req, "code", "");

    ModelResult result = new ModelResult(Result.kBadParam);

    if (StrKit.notBlank(code)) {
      result = Reditor.query(code);
    }

    renderJson(result.toJson());
  }


}
